package com.lzhpo.core.utils;

import com.alibaba.fastjson.JSONObject;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.TimeUnit;

/**
 * [{"issue":"743924","opendate":"2020-01-14 13:30:00","code":"1,3,7,8,10,9,4,5,2,6","lotterycode":"BJPK10","officialissue":"743924"}]
 *
 */

public class HttpUtil {
	private static final MediaType MEDIA_TYPE_JSON = MediaType.parse("application/json; charset=utf-8");// mdiatype

	private static Logger logger = LoggerFactory.getLogger(MyDateUtil.class);

	// 这个需要和服务端保持一致
	private static OkHttpClient okHttpClient;
	private static JSONObject jsonObject;

	private static OkHttpClient getInstance() {
		if (okHttpClient == null) {
			synchronized (HttpUtil.class) {
				if (okHttpClient == null) {
					okHttpClient = new OkHttpClient.Builder().connectTimeout(10, TimeUnit.SECONDS)// 10秒连接超时
							.writeTimeout(10, TimeUnit.SECONDS)// 10m秒写入超时
							.readTimeout(10, TimeUnit.SECONDS)// 10秒读取超时
							// .addInterceptor(new HttpHeaderInterceptor())//头部信息统一处理
							// .addInterceptor(new CommonParamsInterceptor())//公共参数统一处理
							.build();
				}
			}
		}
		return okHttpClient;
	}

	/**
	 * @param url      url地址
	 */
	public static String get(String url) {
		String result="";
		OkHttpClient client = new OkHttpClient();
		Request request = new Request.Builder()
				.url(url)
				.build();

		try (Response response = client.newCall(request).execute()) {
			result= response.body().string();
		}catch (Exception e){
			e.printStackTrace();
			logger.error("current url is --->:{}",url);
		}
		return result;

	}



}